﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using FineUI;
using System.Collections.Generic;

public partial class Manager_SysMenu : System.Web.UI.PageEx
{
    private string strPageCode = "S0000C";
    private string strLoginResult = string.Empty;
    private List<Model.DbSysMenu> lst = new List<Model.DbSysMenu>();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!DalEx.DbManagerEx.CheckLoginRight(UserType, UserId, strPageCode, ref strLoginResult))
        {
            FineUI.Alert.Show(strLoginResult, FineUI.MessageBoxIcon.Warning);
            Response.Redirect("Login.aspx");
        }
        if(!Page.IsPostBack)
        {
            DataBinds();
            btnReset.OnClientClick = sfMenu.GetResetReference();
        }
    }
    protected void DataBinds()
    {
        lst = null;
        lst = new List<Model.DbSysMenu>();
        DalEx.DBSysMenuEx.GetMenuList("0", ref lst);
        ddlParentMenu.DataSource = lst;
        ddlParentMenu.DataTextField = "SmName";
        ddlParentMenu.DataValueField = "SmCode";
        ddlParentMenu.DataSimulateTreeLevelField = "SmLevel";
        ddlParentMenu.DataBind();
        ddlParentMenu.Items.Insert(0,new FineUI.ListItem("顶级菜单", "0"));

        gridList.DataSource = DalEx.DBSysMenuEx.Select(" and 1=1 order by smOrder Asc");
        gridList.DataBind();
    }
    protected void gridDataList_RowDataBound(object sender, FineUI.GridRowEventArgs e)
    {
        DataRowView row = e.DataItem as DataRowView;
        if (row != null)
        { 

        }
    }
    protected void btnAddMenu_Click(object sender, EventArgs e)
    {
        List<int> lstSelectRowId = WebBase.Comm.GetGridSeleteRowId(gridList);
        if (lstSelectRowId.Count == 1)
        {
            Model.DbSysMenu _sysMenu = DalEx.DBSysMenuEx.Select(lstSelectRowId[0]);
            if (_sysMenu.SmId > 0)
            {
                hfMenuId.Text = _sysMenu.SmId.ToString();
                txtMenuName.Text = _sysMenu.SmName;
                txtMenuCode.Text = _sysMenu.SmCode;
                txtMenuOrder.Text = _sysMenu.SmOrder.ToString();
                cboxMenuStatus.Checked = Convert.ToBoolean(_sysMenu.SmStatus);
                if (!string.IsNullOrEmpty(_sysMenu.SmUpCode))
                {
                    ddlParentMenu.SelectedValue = _sysMenu.SmUpCode;
                }
                else
                {
                    ddlParentMenu.SelectedIndex = 0;
                }
                txtMenuUrl.Text = _sysMenu.SmUrl;
            }
            winMenu.Popup = true;
        }
        else if (lstSelectRowId.Count > 1)
        {
            Alert.Show("抱歉，请不要选择多项进行编辑！", MessageBoxIcon.Warning);
        }
        else
        {
            sfMenu.Reset();
            winMenu.Popup = true;
        }
    }
    protected void btnDeleteMenu_Click(object sender, EventArgs e)
    {
        List<int> lstSelectRowId = WebBase.Comm.GetGridSeleteRowId(gridList);
        if (lstSelectRowId.Count > 0)
        {
            DataSet ds = null;
            ArrayList lstSql = null;
            lstSql = new ArrayList();
            Model.DbSysMenu _sysMenu = null;
            foreach (int iSelectId in lstSelectRowId)
            {
                _sysMenu = DalEx.DBSysMenuEx.Select(iSelectId);
                ds = DalEx.DBSysMenuEx.Select(" and smUpCode='" + _sysMenu.SmCode + "'");
                if (SqlHelper.HasRow(ds))
                {
                    lstSql.Clear();
                }
                else
                {
                    lstSql.Add("delete from dbSysMenu where smId=" + iSelectId);
                }
            }
            int iResult = SqlHelper.ExecSql(lstSql);
            if (iResult > 0)
            {
                DataBinds();
                Alert.Show("数据删除成功！", MessageBoxIcon.Information);
            }
            else
            {
                Alert.Show("请检查是否还有下级菜单！", MessageBoxIcon.Warning);
            }
        }
        else
        {
            Alert.Show("请选择要删除项！", MessageBoxIcon.Warning);
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        int iMenuOrder = 0;
        short sMenuStatus = 0;
        int iTempMenuId = 0;
        string strResult = string.Empty;
        string strMenuName = SqlHelper.String(txtMenuName.Text.Trim());
        string strMenuCode = SqlHelper.String(txtMenuCode.Text.Trim());
        string strMenuUrl = SqlHelper.String(txtMenuUrl.Text.Trim());
        Int32.TryParse(SqlHelper.String(txtMenuOrder.Text.Trim()), out iMenuOrder);
        string strUpMenuCode = SqlHelper.String(ddlParentMenu.SelectedValue);
        sMenuStatus = Convert.ToInt16(cboxMenuStatus.Checked);
        Int32.TryParse(SqlHelper.String(hfMenuId.Text.Trim()), out iTempMenuId);

        Model.DbSysMenu sysMenu = new Model.DbSysMenu();
        sysMenu.SmId = iTempMenuId;
        sysMenu.SmName = strMenuName;
        sysMenu.SmCode = strMenuCode;
        sysMenu.SmUpCode = strUpMenuCode;
        sysMenu.SmUrl = strMenuUrl;
        sysMenu.SmOrder = iMenuOrder;
        sysMenu.SmStatus = sMenuStatus;

        if (DalEx.DBSysMenuEx.InsertEx(sysMenu, ref strResult) > 0)
        {
            Model.DbActionLog actionLog = new Model.DbActionLog();
            actionLog.AUId = UserId.ToString();
            actionLog.ADateTime = DateTime.Now;
            actionLog.ANote = "操作系统菜单["+sysMenu.SmName+"]成功！";
            actionLog.AIpaddress = WebBase.GetClientIP.getIPAddress2Num().ToString();
            actionLog.AOS = WebBase.Comm.GetOS;
            actionLog.ABrowse = WebBase.Comm.GetUserBrowser;
            Dal.DbActionLogDal.Insert(actionLog);
            DataBinds();
            Alert.Show(strResult, MessageBoxIcon.Information);
        }
        else
        {
            Alert.Show(strResult, MessageBoxIcon.Warning);
        }
    }
    protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
    {
        int iPageSize = 20;
        int.TryParse(ddlPageSize.SelectedValue, out iPageSize);
        gridList.PageSize = iPageSize;
        DataBinds();
    }
    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        DataBinds();
    }
}
